import { ActionTree } from 'vuex';
import { AppState } from './state';
import { RootState } from '@/store/root-state';
import {get} from "@/request/useRequest";
import {Props} from "@/components/typing";

export const actions: ActionTree<AppState, RootState> = {
    ['setIndiInfo']({ commit },value) {
        return new Promise((resolve, reject) => {
            commit('setIndiInfo', value);
            resolve(value)
        });
    },
    ['getButtonMenu']({ commit }) {
        return new Promise((resolve, reject) => {
            get('authority/list').then(res => {
                let menuMap:Props = {};
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach((item: Props) => {
                        menuMap[item.menuName] = item.authority
                    })
                    commit('setButtonMenu',menuMap);
                }
                resolve(menuMap)
            })
        });
    },
    ['getCurrentRoles']({ commit }) {
        return new Promise((resolve, reject) => {
            get('user/getCurrentRoles').then(res => {
                let roles:Props[] = [];
                if(res.success) {
                    roles = res.data
                    commit('setCurrentRoles',roles);
                }
                resolve(roles)
            })
        });
    },
    ['changeSpinning']({ commit },value) {
        return new Promise(resolve => {
            commit('changeSpinning', value);
            resolve(value)
        });
    }
};
